package sg.edu.nus.iss.vmcs.machinery;

/*
 * Copyright 2003 ISS.
 * The contents contained in this document may not be reproduced in any
 * form or by any means, without the written permission of ISS, other
 * than for the purpose for which it has been supplied.
 *
 */

import java.awt.event.WindowEvent;

import sg.edu.nus.iss.vmcs.store.Store;
import sg.edu.nus.iss.vmcs.store.StoreController;
import sg.edu.nus.iss.vmcs.system.SimulatorControlPanel;
import sg.edu.nus.iss.vmcs.ui.FactoryMaker;
import sg.edu.nus.iss.vmcs.ui.component.Checkbox;
import sg.edu.nus.iss.vmcs.ui.component.Dialog;
import sg.edu.nus.iss.vmcs.ui.component.Font;
import sg.edu.nus.iss.vmcs.ui.component.Label;
import sg.edu.nus.iss.vmcs.ui.component.Panel;
import sg.edu.nus.iss.vmcs.ui.event.awt.AwtWindowAdapter;
import sg.edu.nus.iss.vmcs.ui.event.awt.AwtWindowEvent;

/**
 *
 *
 * @version 3.0 5/07/2003
 * @author Olivo Miotto, Pang Ping Li
 */

public class MachinerySimulatorPanel {

	private static final String TITLE = "Machinery Panel";

	private StoreViewer cashDisplay;
	private StoreViewer drinksDisplay;
	private Checkbox doorDisplay;
	private StoreController storeCtrl;
	private MachineryController machineryCtrl;
	private Dialog dialog;

	public MachinerySimulatorPanel(SimulatorControlPanel scp, MachineryController machCtrl) {
		dialog = FactoryMaker.getFactory().createDialog(scp, TITLE, false);
		
		machineryCtrl = machCtrl;
		storeCtrl = machineryCtrl.getMainController().getStoreController();

		Label lb = FactoryMaker.getFactory().createLabel();
		Font font = FactoryMaker.getFactory().createFont("Helvetica", 1, 24);
		lb.setFont(font);
		lb.setAlignment(Label.CENTER);

		cashDisplay = new StoreViewer(Store.CASH, storeCtrl);
		drinksDisplay = new StoreViewer(Store.DRINK, storeCtrl);

		Panel tp = FactoryMaker.getFactory().createPanel();
		tp.setLayout(FactoryMaker.getFactory().createGridLayout(0, 1));
		tp.add(cashDisplay);
		tp.add(drinksDisplay);

		Panel dp = FactoryMaker.getFactory().createPanel();
		doorDisplay = FactoryMaker.getFactory().createCheckbox();
		doorDisplay.addItemListener(new DoorListener(machineryCtrl));
		doorDisplay.setLabel("Door Locked");
		dp.add(doorDisplay);

		dialog.setLayout(FactoryMaker.getFactory().createBorderLayout());
		dialog.add("North", lb);
		dialog.add("Center", tp);
		dialog.add("South", dp);
		dialog.pack();

		dialog.addWindowListener(new AwtWindowAdapter() {
			public void windowClosing(WindowEvent e) {
				machineryCtrl.closeMachineryPanel();
				super.windowClosing(e);
			}
		});
	}

	public void display() {
		dialog.setVisible(true);
	}

	public void closeDown() {
		dialog.dispose();
	}

	public StoreViewer getCashStoreDisplay() {
		return cashDisplay;
	}

	public StoreViewer getDrinksStoreDisplay() {
		return drinksDisplay;
	}

	public void setDoorState(boolean state) {
		doorDisplay.setState(state);
		this.setActive(!state);

	}

	public void setActive(boolean state) {
		cashDisplay.setActive(state);
		drinksDisplay.setActive(state);
		doorDisplay.setEnabled(state);
	}

}
